package com.xing.action;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import lombok.Getter;
import lombok.Setter;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Controller;

import com.xing.model.ContractInfo;
import com.xing.model.ContractPayment;
import com.xing.model.DicPayment;
import com.xing.model.EmployeeInfo;
import com.xing.service.ContractService;
import com.xing.service.EmployeeService;
import com.xing.service.ProjectService;
import com.xing.util.QueryBuilder;

@Controller
public class ContractAction extends BaseAction {
	/** */
	/**
     * 
     */
	private static final long serialVersionUID = 1L;

	@Resource
	private ContractService contractService;

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	
	@Resource
	private ProjectService projectService;

	@JSON(serialize=false)
	public ProjectService getProjectService() {
		return projectService;
	}
	
	@Resource
	private EmployeeService employeeService;

	@JSON(serialize=false)
	public EmployeeService getEmployeeService() {
		return employeeService;
	}
	
	@JSON(serialize=false)
	public String getContract() throws Exception {
		
		jsonInfo = contractService.getContractInfo(contractId);
		
		return "getObject";
	}

	public String list() throws Exception {

//		List<Map<String, Object>> projectList = (List<Map<String, Object>>) projectService.getListAndCount(null, 1, 20).get("rows");
		List<EmployeeInfo> employeeInfoList = employeeService.getList();
		List<DicPayment> paymentInfoList = contractService.getPaymentDicList();
		
//		getRequest().put("projectList", projectList);
		getRequest().put("employeeInfoList", employeeInfoList);
		getRequest().put("paymentInfoList", paymentInfoList);
		
		return "list";
		
	}
	
	@Override
	public String dataGrid() {
		
		Map<String, Object> map = contractService.getListAndCount(buildQueryCondition(), getPage(), getRows());
		setDataGrid(map);
		
		return "dataGrid";
	} 
	
	public String addContract() throws Exception {
		
		ContractInfo info = createContractInfo();
		contractService.addContract(info);
		
		setMessage("msg", "����ɹ���");
		
		return list();
	}
	
	public String modifyContract() throws Exception {
		
		ContractInfo info = createContractInfo();
		contractService.updateContract(info);
		
		setMessage("msg", "���³ɹ���");
		
		return list();
	}
	
	public String deleteContract() throws Exception {
		
		contractService.deleteContract(getDeleteId());
		
		setMessage("msg", "ɾ��ɹ���");
		
		return list();
	}

	private ContractInfo createContractInfo() {
		
		if (uploadFile != null) {
			System.out.println(uploadFile.getName());
			System.out.println(uploadFileFileName);
		} else {
			System.out.println("upload file null");
		}
		
		ContractInfo info = new ContractInfo();

		info.setContractCode(contractCode);
		info.setContractName(contractName);
		info.setContractContent(contractContent);
		info.setProjectId(projectId);
		info.setAmount(amount);
		info.setCreateTime(createTime);
		info.setCreateUserId(createUserId);
		info.setRemark(remark);
		
		if (contractId > 0) {
			info.setContractId(contractId);
		} 
		
		info.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		info.setUpdateUserId(2);
		
		System.out.println(Arrays.asList(paymentId));
		System.out.println(Arrays.asList(paymentRemark));
		
		Set<ContractPayment> cpSet = new HashSet<ContractPayment>();
		for (int i = 0; i < paymentId.length; i++) {
			ContractPayment cp = new ContractPayment();
			cp.setPaymentId(paymentId[i]);
			cp.setPaymentRemark(paymentRemark[i]);
			cp.setContractId(contractId);
			cp.setContractInfo(info);
			cp.setPaymentOrder(i+1);
			
			cpSet.add(cp);
		}
		info.setContractPaymentSet(cpSet);
		
		
		return info;
	}
	
	
	private List<String> buildQueryCondition() {
		List<String> conditionList = new ArrayList<String>();
		
		QueryBuilder.buildQueryCondition(query_contractName, "c.contractName", QueryBuilder.LIKE, conditionList);
		QueryBuilder.buildQueryCondition(query_contractCode, "c.contractCode", QueryBuilder.LIKE, conditionList);
		QueryBuilder.buildQueryCondition(query_projectId, "c.projectId", QueryBuilder.EQUAL, conditionList);
		QueryBuilder.buildTimeQueryCondition(query_startCreateTime, query_endCreateTime, "c.createTime", conditionList);
		
		return conditionList;
	}
	
	
	private Map<String, Object> jsonInfo;
	@JSON(name="contractInfo")
	public Map<String, Object> getJsonInfo() {
		return jsonInfo;
	}
	
	
	@Getter @Setter private int contractId;
	@Getter @Setter private String contractName;
	@Getter @Setter private String contractCode;
	@Getter @Setter private String contractContent;
	@Getter @Setter private int projectId;
	@Getter @Setter private double amount;
	@Getter @Setter private Timestamp createTime;
	@Getter @Setter private int createUserId;
	@Getter @Setter private String remark;
	@Getter @Setter private int[] paymentId;
	@Getter @Setter private String[] paymentRemark;
	@Getter @Setter private File uploadFile;
	@Getter @Setter private String uploadFileFileName;
	
	
	@Getter @Setter private String query_contractName;
	@Getter @Setter private String query_contractCode;
	@Getter @Setter private String query_projectId;
	@Getter @Setter private String query_projectName;
	@Getter @Setter private String query_startCreateTime;
	@Getter @Setter private String query_endCreateTime;
	
	
	
}
